package model

import (
	"context"
	"demo/internal/errorx"
	"fmt"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ OptionModel = (*customOptionModel)(nil)

type (
	// OptionModel is an interface to be customized, add more methods here,
	// and implement the added methods in customOptionModel.
	OptionModel interface {
		optionModel
		withSession(session sqlx.Session) OptionModel
		SelectBuilder() squirrel.SelectBuilder
		BulkInsert(context context.Context,session sqlx.Session,options []*Option)error
		FindList(ctx context.Context,session sqlx.Session,serveyId int64)([]*Option,error)
		DeleteByQuestionId(ctx context.Context, session sqlx.Session,questionId int64)error
		FindListByQid(ctx context.Context,session sqlx.Session,qId int64)([]*Option,error)
	}

	customOptionModel struct {
		*defaultOptionModel
	}
)

// NewOptionModel returns a model for the database table.
func NewOptionModel(conn sqlx.SqlConn) OptionModel {
	return &customOptionModel{
		defaultOptionModel: newOptionModel(conn),
	}
}

func (m *customOptionModel) withSession(session sqlx.Session) OptionModel {
	return NewOptionModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customOptionModel) BulkInsert(context context.Context,session sqlx.Session,options []*Option)error{
	conn := sqlx.NewSqlConnFromSession(session)
	query := fmt.Sprintf("insert into public.option (question_id,op_content,is_true,servey_id) values (? ,? ,?, ?)")
	bulk,err := sqlx.NewBulkInserter(conn,query)
	if err != nil {
		return errorx.NewBizError(500,"批量插入问题选项失败")
	}
	defer bulk.Flush()
	for _,val := range options {
		fmt.Println("插入的数据是：",val)
		bulk.Insert((*val).QuestionId,(*val).OpContent,(*val).IsTrue,(*val).ServeyId)
	}
	return nil
}

func (m *customOptionModel) SelectBuilder() squirrel.SelectBuilder {
	return squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).Select("option.*").From(m.table)
}

func (m *customOptionModel)FindListByQid(ctx context.Context,session sqlx.Session,qId int64)([]*Option,error){
	builder := m.SelectBuilder().Where(squirrel.Eq{"question_id":qId})
	query,values,err := builder.ToSql()
	if err != nil {
		return nil,err
	}
	var options []*Option
	err = m.conn.QueryRowsPartialCtx(ctx,&options,query,values...)
	if err != nil {
		return nil,err
	}
	return options,nil
}

func (m *customOptionModel)FindList(ctx context.Context,session sqlx.Session,serveyId int64)([]*Option,error){
	builder := m.SelectBuilder().Where(squirrel.Eq{"servey_id": serveyId})
	query,values,err := builder.ToSql()
	if err != nil {
		return nil,err
	}
	var resp []*Option
	if session != nil {
		err = session.QueryRowsPartialCtx(ctx,&resp,query,values...)
	}else{
		err = m.conn.QueryRowsPartialCtx(ctx,&resp,query,values...)
	}
	return resp,err
}

func (m *customOptionModel)DeleteByQuestionId(ctx context.Context, session sqlx.Session,questionId int64)error{
	query := fmt.Sprintf("delete from public.option where question_id = $1")
	var err error
	if session != nil {
		_,err = session.ExecCtx(ctx,query,questionId)
	}else{
		_,err = m.conn.ExecCtx(ctx,query,questionId)
	}

	return err
}

